//
//  YWBuyerDemandCell.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/27.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//

#import "YWBuyerDemandCell.h"
#import <UIImageView+WebCache.h>
#import "YWBuyerDemand.h"
#import "YWUserInfo.h"
#import <UIImageView+WebCache.h>

@interface YWBuyerDemandCell()

@property (weak, nonatomic) IBOutlet UIImageView *imageVIew;

@property (weak, nonatomic) IBOutlet UILabel *describeLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *photoNumLabel;

@end

@implementation YWBuyerDemandCell

- (void)awakeFromNib {

    self.backgroundColor = [UIColor whiteColor];
}

- (void)setBuyerDemand:(YWBuyerDemand *)buyerDemand{
    _buyerDemand = buyerDemand;
    
    // 可能会没图片
    if (_buyerDemand.urls.count > 0) {
        NSURL *url = [NSURL URLWithString:[self choosePhotoUrl:_buyerDemand.urls[0]]];
        [self.imageVIew sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_pic"]];

    }else{
        self.imageVIew.image = [UIImage imageNamed:@"default_pic"];
    }
    
    // 设置内容
    self.describeLabel.text = _buyerDemand.sampleDescribe;
    
    // 设置时间
    self.timeLabel.text = [self getTime:_buyerDemand.createTimeFormat];
    
    
    //设置图片数量
    if (_buyerDemand.urls.count > 1){
        self.photoNumLabel.hidden = NO;
        self.photoNumLabel.text = [NSString stringWithFormat:@"%zd张图",_buyerDemand.urls.count];
    }else{
        self.photoNumLabel.hidden = YES;
    }
}

// 选中高中低三个质量的图片
- (NSString *)choosePhotoUrl:(NSString *)photoUrl{
    
    NSString *result = nil;
    
    YWUserInfo *useInfo = [YWUserInfo sharedUseInfo];
    NSInteger photoQuality = [useInfo.photoQuality intValue];
    
    if (photoQuality == 0) {    // 高
        result = [NSString stringWithFormat:@"%@@!l500sauto95 ",photoUrl];
    }else if (photoQuality == 1) {    // 中
        result = [NSString stringWithFormat:@"%@@!l200sauto95",photoUrl];
    }else if (photoQuality == 2) {    // 低
        result = [NSString stringWithFormat:@"%@@!l100sauto75 ",photoUrl];
    }
    
//    NSLog(@"%@",result);
    return result;
}

// 计算时间
- (NSString *)getTime:(NSString *)timeString{
    NSString *day = nil;
    NSString *time = nil;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [formatter dateFromString:timeString];
    
    if ([date isToday]) {   // 今天
        day = @"今天";
    }else if([date isYesterday]){  // 昨天
        day = @"昨天";
    }else{      // 其他 年月日
        formatter.dateFormat = @"yyyy-MM-dd";
        day = [formatter stringFromDate:date];
    }
    
    formatter.dateFormat = @"HH:mm";
    time = [formatter stringFromDate:date];
    
    return [NSString stringWithFormat:@"%@ %@",day,time];
}



@end
